<?php
/**
 * 产品属性模型
 *
 * @copyright   Copyright 2011, Richie
 * @author      Richie <richbossman@163.com>
 * @package     SMS
 * @link        http://www.letphp.com
 * 
 * $Id: AttributeModel.class.php 80 2011-04-18 13:34:13Z 442767452@qq.com $
 */
class AttributeModel extends SMSModel {
	//数据验证
	protected $_validate	=	array(
		array('name','checkName', '名称已经存在！',0,'callback'),
	);
	//自动完成
	protected $_auto = array(
		array('add_time', 'time', 1, 'function'),
		array('update_time', 'time', 3, 'function'),
		array('status', '1')
	);
	
	//检查应用名称是否存在
	function checkName(){
		$map['name'] = $_POST['name'];
        if(!empty($_POST['id'])) {
			$map['id']	=	array('neq',$_POST['id']);
        }
        $map['parent_id'] = array('eq', $_POST['parent_id']);
		$map['type'] = array('eq', $_POST['type']);
		$result	=	$this->where($map)->field('id')->find();
        if($result) {
        	return false;
        }else{
			return true;
		}
	}
	
	//取得属性位置
	function getAttributePosition($id=0){
		static $attribute = array();
		$parent = $this->where('id='.$id)->field('id,parent_id,name')->find();
		if($parent){
			$attribute[] = $parent['name'];
		}
		if($parent['parent_id']){
			$this->getAttributePosition($parent['parent_id'], true);
		}
		return implode(" > ", array_reverse($attribute));
	}
	
	//取指定属性的所有子类
	function getAttribute($id=0, $isall=false, $isselect=false){
		$map['parent_id'] = $id;
		if(!$isall) $map['status'] = 1;
		if($isselect){
			$map['value_type'] = 1;
		}elseif($isselect === 0){
			$map['value_type'] = 0;
		}
		return $this->where($map)->order('sort ASC,id ASC')->getField('id,name');
	}
	
	//取得属性组下面的属性与属性值
	function getAttributeGroup($id=0, $isselect=true){
		$attribute = array();
		$attr = $this->getAttribute($id, true, $isselect);
		foreach($attr as $key=>$name){
			$attribute[$key]['name'] = $name;
			$attribute[$key]['id'] = $key;
			$attribute[$key]['children'] = $this->getAttribute($key, true);
		}
		return $attribute;
	}
}
?>